library(plm)
library(AER)
library(foreign)
library(readstata13)

setwd("~/Desktop/QJPS Replication")

data <- read.csv("all_countries_ideal.csv")

countries<-unique(data$cow)

#create an empty list
data_2<-list()

#go country by country and put each country in a "basket"
for(i in 1:length(countries)){
  
  sub<-data[data$cown==countries[i],]
  
  sub$cons_num<-1:nrow(sub)
  
  data_2[[i]]<-sub
  
}

#Turn that into a dataframe
data<-do.call(rbind,data_2)

data<-pdata.frame(data,index=c("cown","cons_num"))

names(data)[6]<-c("cow")

#get the original constitutions data, are they amendments, new, interrim constitutions, reinstated
dd<-read.dta13(file="data/ccpcnc_v2_small.dta")

dd<-dd[dd$evnt==1,]

dd<-dd[,c("cowcode","evntyear","evnttype")]

names(dd)<-c("cow","year","evnttype")

####################################################################

#create a panel
out_dat<-list()

for(i in 1:length(countries)){
  
  sub<-data[data$cow==countries[i],]
  
  yrs<-unique(sub$year)
  
  yrs<-seq(min(yrs),2010,1)
  
  cscore<-rep(NA,length(yrs))
  
  for(j in 1:nrow(sub)){
    
    cscore<-ifelse(yrs >= sub[j,]$year, sub[j,]$median,cscore)
    
  }
  
  sub_dat<-data.frame(cbind(cscore,yrs))
  
  sub_dat[,3:6]<-sub[1,5:8]
  
  names(sub_dat)[2]<-"year"
  names(sub_dat)[5]<-"min_year"
  
  out_dat[[i]]<-sub_dat
   
}

#turn this into a dataframe
data_p<-do.call(rbind,out_dat)  

data_p<-merge(data_p,dd,by=c("cow","year"),all.x=T)
data_p$evnttype<-ifelse(is.na(data_p$evnttype),"none",data_p$evnttype)

####################################################################

#boix, miller, rosatto data of democracy
bmr<-read.csv("data/democracy-v2.0.csv")

names(bmr)<-c("country","cow","abbreviation","year","democracy","sovereign","democracy_trans","democracy_breakdowns","democracy_duration","democracy_omitteddata")

bmr$trans_to_democracy<-ifelse(bmr$democracy_trans==1,1,0)
bmr$away_from_democracy<-ifelse(bmr$democracy_trans=="+AC0-1",1,0)

data_p<-merge(data_p,bmr,by=c("cow","year"),all.x=T)

####################################################################

#GDP dataset
dem_dev <- read.dta("data/DemDev_SI.dta")[,c("ccode","year","loggdp")]

names(dem_dev)<-c("cow","year","loggdp")

data_p<-merge(data_p,dem_dev,by=c("cow","year"),all.x=T)

####################################################################

#polity dataset
polity<-read.csv(file="data/polityiv.csv", fileEncoding="UTF-16LE")

polity$foreign_occ<-ifelse(polity$democ==-66,1,0)

polity$breakdown_order<-ifelse(polity$democ==-77,1,0)


polity<-polity[,c("ccode","year","foreign_occ","breakdown_order")]

names(polity)<-c("cow","year","foreign_occ","breakdown_order")

data_p<-merge(data_p,polity,c("cow","year"),all.x=T)

########################################################

#quality of government data
qog<-read.csv("data/qog_std_ts_jan17.csv")
qog2 <- subset(qog, select = c("year", "cname", "ht_colonial", "ccodecow", "al_ethnic", "al_religion", "al_language"), !is.na(ht_colonial))

m <- match(data_p$cow, qog2$ccodecow)
table(is.na(m))

data_p$ht_colonial <- qog2$ht_colonial[m]
data_p$al_ethnic <- qog2$ht_colonial[m]
data_p$al_religion <- qog2$al_religion[m]
data_p$al_language <- qog2$al_language[m]

data_p<-pdata.frame(data_p,index=c("cow","year"))

########################################################

#create differences variables
#political order
data_p$resume_order<-ifelse(diff(data_p$breakdown_order)==-1,1,0)
data_p$stop_order<-ifelse(diff(data_p$breakdown_order)==1,1,0)

#occupations
data_p$stop_occ<-ifelse(diff(data_p$foreign_occ)==-1,1,0)
data_p$start_occ<-ifelse(diff(data_p$foreign_occ)==1,1,0)

#colonized at all
data_p$col_dummy <- ifelse(data_p$ht_colonial != 0, 1, 0)

#state duration
data_p$state_duration<-(as.numeric(as.character(data_p$year))-as.numeric(as.character(data_p$min_year)))

####################################################################

#change reference factor for event type
data_p$evnttype<-relevel(as.factor(data_p$evnttype),ref="none")
#dummy for new constitution
data_p$new_cons<-ifelse(data_p$evnttype=="new",1,0)
#dummy for amend constitutions
data_p$amend_cons<-ifelse(data_p$evnttype=="amendment",1,0)

data_p$year <- as.numeric(as.character(data_p$year))
data_p$cow <- as.numeric(as.character(data_p$cow))

write.csv(data_p, "all_countries_ideal_points_panel.csv")
write.dta(data_p, "all_countries_ideal_points_panel.dta")



#within versus across unit variance
with <- lm(cscore ~ as.factor(cow), data = data_p)
within_resid <-resid(with)
within_var <- sum(within_resid^2)/(length(within_resid) - length(unique(data_p$cow)))
total_var <- var(data_p$cscore)
between_var <- total_var - within_var
within_var/total_var